package com.sanerzone.smscenter.gateway.http.handler.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sanerzone.smscenter.biz.message.SMSMOMessage;
import com.sanerzone.smscenter.biz.message.SMSMTMessage;
import com.sanerzone.smscenter.biz.message.SMSRTMessage;
import com.sanerzone.smscenter.biz.message.SMSSRMessage;
import com.sanerzone.smscenter.biz.utils.StringHelper;
import com.sanerzone.smscenter.common.tools.HttpRequest;
import com.sanerzone.smscenter.gateway.http.handler.GateWayHttpAbstract;
import com.sanerzone.smscenter.gateway.http.message.SmsHttpMsgResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 千橡老平台
 */
public class SmsApiQXOld extends GateWayHttpAbstract {

	public SmsApiQXOld(String gateWayID, String jsonParams) {
		super(gateWayID, jsonParams);
	}

	@Override
	@SuppressWarnings("rawtypes")
	public List<SMSSRMessage> doSend(SMSMTMessage mt) {
		Map<String, String>  params = Maps.newHashMap();
		params.put("account", paramsMaps.get("account"));
		params.put("password", paramsMaps.get("password"));
		params.put("mobile", mt.getPhone());
		params.put("content", mt.getSmsContent());
		params.put("longSms", paramsMaps.get("longSms"));
		String json = HttpRequest.sendGet(paramsMaps.get("url") + "/submit", Joiner.on("&").withKeyValueSeparator("=").join(params), null);

		logger.info("网关:{}, 请求:{}, 响应:{}", this.gateWayID, JSON.toJSONString(params), json);

		List<SMSSRMessage> resultList = Lists.newArrayList();

		SMSSRMessage message = initGatewayResultMessage(json, mt, ",");
		String resultCode = null;
		String resultMessage = null;
		String taskid = null;

		if (StringUtils.equals(message.getResult(), "F10199")) {
			try {
				String[] object = json.split(",");
				resultCode = object[0];
				resultMessage = object[1];
				if(StringUtils.equals(resultCode, "0")) {
					taskid = object[2].trim();
				}
			} catch (Exception e) {
				logger.error("解析错误", e);
			}
			message.setMsgkey("");
			setGatewayResultCode(message, taskid, resultCode, resultMessage, "0");
		}

		resultList.add(message);
		return resultList;
	}

	@Override
	public SmsHttpMsgResponse parseReport(String text, List<SMSRTMessage> rtList) {
		logger.info("{}网关推送状态报告:{}", gateWayID, text);

		if(StringUtils.isNotBlank(text)) {
//			smsMsgId=100001200101130511090326000001&status=succeed

			//Map<String, Object> resultMap = JSON.parseObject(text, Map.class);
			String[] object = text.split("\n");
			for (int i = 1; i < object.length ; i++) {
				String data = object[i];
				if (StringHelper.isNotBlank(data)) {
					String [] resutl = data.split(",");
					SMSRTMessage smsRtMessage = initSmsRtMessage(resutl[0], null, resutl[1],
							resutl[2], "1");
					rtList.add(smsRtMessage);
				}
			}

    	}

		return new SmsHttpMsgResponse("0");
	}

	@Override
	public SmsHttpMsgResponse parseDeliver(String text,
			List<SMSMOMessage> moList) {
		return null;
	}

	@Override
	public String queryReport(String jsonText) {
		return null;
	}


	public String queryDeliver(String jsonText) {
		return null;
	}
	@Override
	public String balance()
	{
		Map<String, String> params = Maps.newHashMap();
		params.put("account", paramsMaps.get("account"));
		params.put("password", paramsMaps.get("password"));
		String result = HttpRequest.sendGet(paramsMaps.get("url") + "/balance", Joiner.on("&").withKeyValueSeparator("=").join(params), null);
		System.out.println("balance result:" + result);
		return "未实现";
	}

	public static void main(String[] args) {
		//{"templateId":"201706091122","paramValue":["123456","5"]}
		String apiUrl = "http://211.138.118.14:1100/sms/";
		//String apiUrl = "http://aep.cmccopen.cn/sms/";

		//String notifyURL = "/api/sms/report/pr/cmcc";

		String jsonParams = "{\"account\":\"cyx001\",\"longSms\":\"1\",\"password\":\"c123123\",\"url\":\"http:// 101.37.77.0:8060/api/\"}";
		SmsApiQXOld api = new SmsApiQXOld("QXOLD",
				jsonParams);
		api.send();
//		api.Test();
//		api.querySms();
//		api.report2();

	}
	public void  send() {
		System.out.println(System.currentTimeMillis());
		Map<String, Object> patams = Maps.newHashMap();
		//本地
		String userid = "10022";
		String appid = "15";
		String ts = System.currentTimeMillis() +   "";
		String apikey = "283d919c807c483a898bcb329c5f490d";
		String apiUrl = "http://127.0.0.1:8181/api/template/sms/send";

	/*	//线上
		String userid = "10029";
		String appid = "18";
		String ts = System.currentTimeMillis() +   "";
		String apikey = "25433103025b41ef84d82b3c0e3142d0";
		String apiUrl = "http://101.37.77.0:8064/api/template/sms/send";*/

		String sign = DigestUtils.md5Hex(userid + appid + ts +apikey);
//		String mobile = "13057755333";
		String mobile = "18268846802,18268846803,18268846804";
		String msgcontent = "【千橡科技】第三方第三方：123457，3分钟内有效，如非本人操作，请忽略！";
//		String msgcontent = "验证的码为：635611";
//		String templateid = "13";
//		String[] templateparam = {"123456","5"};
		String extnum = "";
		String time = "";
		String messageid = "test123456789";

//		String apiUrl = "http://127.0.0.1:8081/api/template/sms/send2";
		patams.put("userid",userid);
		patams.put("appid",appid);
		patams.put("ts",ts);
		patams.put("sign",sign);
		patams.put("mobile",mobile);
//		patams.put("templateid",templateid);
		patams.put("msgcontent",msgcontent);
//		patams.put("templateparam",templateparam);
		patams.put("extnum",extnum);
		patams.put("time",time);
		patams.put("messageid",messageid);
		patams.put("gatewayId","HT0001");

		String result = HttpRequest.sendPostJson(apiUrl,JSON.toJSONString(patams), null, 3000 );
		System.out.println(result);
	}

	public void  querySms() {
		Map<String, String > params = Maps.newHashMap();
		String account = "cyx001";
		String password= "c123123";
		params.put("account",account);
		params.put("password",password);

		String apiUrl = "http://42.121.1.167:8080/report";
		String result = HttpRequest.sendGet(apiUrl, Joiner.on("&").withKeyValueSeparator("=").join(params),null);
		System.out.println("query sms result :" + result + ",长度：" + result.length());
		String[] object = result.split("\n");
		for (int i = 1; i < object.length ; i++) {
			System.out.println("yuansu:" + object[i]);
			String data = object[i];
			if (StringHelper.isBlank(data)) {

			}
		}
	}
	public  void report2() {
		String a = "0,3\n" +
				"20130812111111\t13588443322\t1234567890\t你好1\n" +
				"20130812111211\t13512345678\t1234567891\t你好2\n" +
				"20130812111311\t13523456789\t1234567892\t你好3\n";
		HashMap<String, String> header = Maps.newHashMap();
		header.put("Content-type","");
		String result = HttpRequest.sendPost("http://101.37.77.0:8062/api/sms/notify/nr/QXOLD",a, header);
		System.out.println("report result :" + result);
	}

	public void Test() {
		String apiUrl = "http://127.0.0.1:8181/smscenter-service/api/sms/report/pr/HT0002";
		String b = "0,1\n" +
				"68157213,18268846802,1,MN:0174";
		String a = "0,1\n" +
				"68128484,18268846802,2,MN:0174";
		String result = HttpRequest.sendPost(apiUrl, b,null);
		System.out.println("result=" + result);
	}
}